﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WR.BL.BCC;
using WR.BL.BE;
using System.Collections.Generic;

namespace WR.UI.MobWebApp
{
    public partial class conscot : System.Web.UI.Page
    {
        private UserBE _objUsr;

        private void checkLogin()
        {
            lblErrorMsg.InnerHtml = string.Empty;
            _objUsr = (UserBE)Session["UserBE"];
            if (_objUsr == null)
            {
                Response.Redirect("login.aspx");
            }
        }

        private void cargarCombos()
        {
            List<GenericBE> lstClaseDoc = (List<GenericBE>)Session["lstClaseDoc"];
            if (lstClaseDoc == null || lstClaseDoc.Count == 0)
            {
                lstClaseDoc = SalesOrderBC.getInstance(Session.SessionID).getClaseDoc(string.Empty, string.Empty);
                Session["lstClaseDoc"] = lstClaseDoc;
            }
            lbAuart.DataSource = lstClaseDoc;
            lbAuart.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.checkLogin();

            if (!Page.IsPostBack)
            {
                this.cargarCombos();

                if (string.IsNullOrEmpty(txtFech1.Value.Trim()))
                {
                    txtFech1.Value = DateTime.Today.AddDays(-7).ToShortDateString().Replace('/','.');
                }
                if (string.IsNullOrEmpty(txtFech2.Value.Trim()))
                {
                    txtFech2.Value = DateTime.Today.ToShortDateString().Replace('/', '.');
                }
                
                //---
                List<SalesOrderBE> lstSO = (List<SalesOrderBE>)Session["lstSO"];
                if (lstSO != null)
                {
                    lvOfertas.DataSource = lstSO;
                    lvOfertas.DataBind();
                }
            }
        }

        protected void btnFind_Click(object o, EventArgs e)
        {
            try
            {
                string strNumdoc = txtNumdoc.Value.Trim();
                string strClasedoc = lbAuart.SelectedValue.Trim();// txtClaseDoc.Value.Trim();
                string strFech1 = txtFech1.Value.Trim();
                string strFech2 = txtFech2.Value.Trim();

                if (string.IsNullOrEmpty(strClasedoc)) { throw new Exception("Falta especificar Clase doc."); }

                _objUsr = (UserBE)Session["UserBE"];
                List<SalesOrderBE> lstSO = SalesOrderBC.getInstance(Session.SessionID).find(_objUsr.Tienda, strClasedoc, strNumdoc, strFech1, strFech2);
                lvOfertas.DataSource = lstSO;
                lvOfertas.DataBind();
                Session["lstSO"] = lstSO;
            }
            catch (Exception ex)
            {
                lblErrorMsg.InnerHtml = ex.Message;
            }
        }
    }
}
